// create by dplord at 2017-02-05 16:14
#include "stdlib.h"
#include "stdio.h"
#include "unistd.h"
#include "sys/socket.h"
#include "arpa/inet.h"
#include "string.h"


void error_handling(char *message) {
    fputs(message, stderr);
    fputc('\n', stderr);
    exit(1);
}
int main(int argc, char **arrv) {

    int serv_sock;
    int client_sock;

    struct sockaddr_in serv_addr;
    struct sockaddr_in client_addr;

    serv_sock = socket(PF_INET, SOCK_STREAM, 0);
    if(serv_sock == -1) {
        error_handling("socket create error");
    }
    memset(&serv_addr, 0, sizeof(serv_addr));
    serv_addr.sin_family = AF_INET;
    serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
    serv_addr.sin_port = htons(9001);

    if(bind(serv_sock, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) == -1) {
        error_handling("bind error");
    }
    if(listen(serv_sock, 5) == -1) {
        error_handling("listen error");
    }

    socklen_t client_addr_size = sizeof(client_addr);
    client_sock = accept(serv_sock, (struct sockaddr *)&client_addr, &client_addr_size);
    if(client_sock == -1) {
        error_handling("accept error");
    }

    char message[] = "hello world";
    write(client_sock, message, sizeof(message));
    close(client_sock);


    return 0;
}